package com.yirui.serialcomm.core.parse;

import com.yirui.serialcomm.core.domain.ASN1SerialCommPacket;
import com.yirui.serialcomm.core.domain.SerialCommPacket;
import com.yirui.serialcomm.core.domain.StreamType;
import com.yirui.serialcomm.core.util.HexUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Dujie
 * @create 2025/8/13
 * @since 1.0.0
 */
@Slf4j
public class ASN1PacketParser extends SimplePacketParser {

    @Override
    public ASN1SerialCommPacket decode(InputStream inputStream) throws IOException {
        byte[] buffer = super.decode(inputStream);
        return ASN1SerialCommPacket.getInstance(buffer);
    }

    // 发送数据（简化协议）
    public byte[] encode(SerialCommPacket packet) throws IOException {

       byte[] data = packet.getType()==StreamType.TCP?ASN1SerialCommPacket.packWithTCP(packet.getData()):ASN1SerialCommPacket.packWithUDP(packet.getData());
        packet.setData(data);
       return super.encode(packet);
    }


}
